﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pomodoro.Core.View;
using Pomodoro.Core.Model;
using Pomodoro.Core.Model.Domain;

namespace Pomodoro.Core.Presenter
{
    public class PomodoroPresenter
    {
        private TaskModel model;
        private TaskView view;

        public PomodoroPresenter()
        {
            model = new TaskModel();
        }

        public PomodoroPresenter(TaskView view)
        {
            model = new TaskModel();
            this.view = view;
        }

        public void AddTask(string description)
        {
            string test = new AlertsHelper().GetAlert(Alerts.CampoTarefaObrigatorio);
            if (description.Equals(String.Empty))
                view.Alerts = test;
            else
            {
                model.SaveNewTask(description);
                view.ListTasks = model.AllTasks();
                view.Alerts = new AlertsHelper().GetAlert(Alerts.TarefaAddSucesso);
            }
        }

        public void AddTask()
        {
            string test = new AlertsHelper().GetAlert(Alerts.CampoTarefaObrigatorio);
            if (view.NewTask.Equals(String.Empty))
                view.Alerts = test;
            else
            {
                model.SaveNewTask(view.NewTask);
                view.ListTasks = model.AllTasks();
                view.Alerts = new AlertsHelper().GetAlert(Alerts.TarefaAddSucesso);
            }
        }

        public Task GetTaskByDescription(string description)
        {
            return model.FindTaskByDescription(description);
        }

        public void CancelTask(string canceledTask)
        {
            int affected = model.DeleteTask(canceledTask);
            if (affected > 0)
                view.Alerts = new AlertsHelper().GetAlert(Alerts.TarefaCanceladaSucesso);
            else
                view.Alerts = new AlertsHelper().GetAlert(Alerts.TarefaNaoExiste);

            view.ListTasks = model.AllTasks();
        }

        public void StartTask(int taskID)
        {
            model.StartTask(taskID);

        }

        public void CheckTime(long currentTime)
        {
            Task taskStated = (from t in view.ListTasks where t.Started == true select t).FirstOrDefault();
            if (taskStated.EndOfTaskTime(currentTime))
            {
                taskStated.End();
                model.UpdateTask(taskStated);
                view.ListTasks = model.AllTasks();
                view.Alerts = new AlertsHelper().GetAlert(Alerts.TarefaConcluida);
            }
        }

        public void Clear()
        {
            model.Clear();
        }

        public string EndTask(int p)
        {
            model.EndTask(p);
            return new AlertsHelper().GetAlert(Alerts.TarefaConcluida);
        }
    }
}
